<template>
  <!-- 编辑生日组件 -->
  <div>
    <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" :min-date="minDate" :max-date="maxDate"
      @cancel="$emit('closePopup')" @confirm="onConfirm" />
  </div>
</template>

<script>
import { absoluteFormatDate } from '@/filters/index.js'
import { setUserprofile } from '@/api/user.js'
export default {
  data () {
    return {
      // 选中时间
      currentDate: new Date(this.birthday),
      // 最小时间
      minDate: new Date('1900-01-01'),
      // 最大时间
      maxDate: new Date()
    }
  },
  props: {
    // 原生日
    birthday: {
      type: String,
      require: true
    }
  },
  model: {
    prop: 'birthday'
  },
  methods: {
    // 确定
    async onConfirm (val) {
      // 格式化时间
      const data = absoluteFormatDate(val, 'YYYY-MM-DD')
      this.$toast.loading({
        message: '修改中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await setUserprofile({
          birthday: data
        })
        this.$toast('修改生日成功')
        // 关闭弹出层
        this.$emit('closePopup')
        // 同步视图
        this.$emit('input', data)
      } catch {
        this.$toast('修改生日失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
